﻿//*** Guidance generated code ***//
using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Globalization;
using System.Linq;

using ProjectBase.Core;
using $safeprojectname$;

namespace $safeprojectname$
{
    public class TextOrInput : TextPropertyControl<object, TextOrInput>
    {
        #region members
        bool? _isDateTime;
        int? _maxLength;
        #endregion members

        #region constructor
        public TextOrInput() { }
        public TextOrInput(bool? asReadonly = null, int? maxLength = null)
            : base(asReadonly)
        {
            _maxLength = maxLength;
        }
        public TextOrInput(string prefix) : base(prefix) { }
        #endregion constructor

        #region Render
        protected override void ReadControls()
        {
            Controls.Add
                (
                    new Div()
                        .SetCssClassName(GetAttribute(Attr.Class))
                        .AddControls
                        (
                            new Literal(Text)
                        )
                );
        }

        protected override void WriteControls()
        {
            Controls.Add
                (
                    new Input(InputType.Text)
                        .SetCssClassName(GetAttribute(Attr.Class))
                        .AddCssClassName(IsDateTime ? "left dateTime" : string.Empty)
                        .SetMaxLength(MaxLength)
                        .SetValue(Text)
                        .SetName(InputName)
                );
            if (IsDateTime)
            {
                Controls.Add
                    (
                        new Input(InputType.Hidden)
                            .SetValue(DateFormat)
                            .SetCssClassName(CultureInfo.CurrentCulture.Name)
                    );
            }
        }
        #endregion Render

        #region Set
        /// <summary>
        ///     Allows to restrict the MaxLength of the Text value.
        ///     If not set, value will be adjusted on client; see: validate.js
        /// </summary>
        /// <param name = "maxLength">If not set, value will be adjusted on client see; validate.js</param>
        /// <returns>this</returns>
        public virtual TextOrInput SetMaxLength(int maxLength)
        {
            MaxLength = maxLength;
            return this;
        }

        /// <summary>
        ///     Explicitly sets if there should be DateTime picker or not.
        ///     Default value is based on the SourceProperty type
        /// </summary>
        /// <param name = "isDateTime"></param>
        /// <returns></returns>
        public virtual TextOrInput ShowAsDateTime(bool isDateTime)
        {
            IsDateTime = isDateTime;
            return this;
        }
        #endregion Set

        #region properties
        /// <summary>
        ///     Allows to restrict the MaxLength of the Text value.
        ///     If not set, value will be adjusted on client; see: validate.js
        /// </summary>
        public virtual int? MaxLength
        {
            get { return _maxLength; }
            set { _maxLength = value; }
        }

        public virtual bool IsDateTime
        {
            get
            {
                if (_isDateTime.Is())
                {
                    return _isDateTime.Value;
                }
                if (Type.IsNull())
                {
                    return false;
                }
                if (Type.IsEquivalentTo(typeof(DateTime))
                    || Type.IsEquivalentTo(typeof(DateTime?)))
                {
                    return true;
                }
                return false;
            }
            set { _isDateTime = value; }
        }

        protected virtual string DateFormat
        {
            get
            {
                return CultureInfo
                    .CurrentCulture
                    .DateTimeFormat
                    .ShortDatePattern
                    .ToLower()
                    .Replace("yy", "y");
            }
        }
        #endregion properties
    }
}